package com.wealth.system.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;

/**
 * 角色
 * @author chan
 */
@Entity
@Table(name="system_role")
public class Role implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	private Long id;
	private String name;
	private String description;
	
	private Integer status;
	private Corporation corp;
	private String creator;
	private Date createtime;
	public List<User> userList = new ArrayList<User>(); 
	public List<Permission> permissionList = new ArrayList<Permission>(); 
	
	/* ---------------- method ----------------*/
	
	public Role() {}
		
	public Role(Long id) {
		this.id = id;
	}
	
	/**
	 * 获取权限id集合
	 */
	@Transient
	public List<String> getPermissionIdList() {
		List<String> permissionIdList = Lists.newArrayList();
		for (Permission p : permissionList) {
			permissionIdList.add(p.getId().toString());
		}
		return permissionIdList;
	}

	public void setPermissionIdList(List<String> permissionIdList) {
		permissionList = Lists.newArrayList();
		for (String id : permissionIdList) {
			Permission p = new Permission();
			p.setId(Long.valueOf(id));
			permissionList.add(p);
		}
	}

	/**
	 * 获取权限id字符串
	 */
	@Transient
	public String getPermissionIds() {
		return StringUtils.join(getPermissionIdList(), ",");
	}
	
	public void setPermissionIds(String permissionIds) {
		permissionList = Lists.newArrayList();
		if (permissionIds != null){
			String[] ids = StringUtils.split(permissionIds, ",");
			setPermissionIdList(Lists.newArrayList(ids));
		}
	}
	
	/**
	 * 获取权限标示字符串列表
	 */
	@Transient
	public List<String> getPermissions() {
		List<String> permissions = Lists.newArrayList();
		for (Permission p : permissionList) {
			if (p.getPermission()!=null && !"".equals(p.getPermission())){
				permissions.add(p.getPermission());
			}
		}
		return permissions;
	}
	
	
	/* -------------------------------- getter/setter --------------------------------*/
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@ManyToMany
	@JoinTable(name = "system_role_permission", 
		joinColumns = { @JoinColumn(name = "role_id") },
		inverseJoinColumns = { @JoinColumn(name = "permission_id") }
	)
	public List<Permission> getPermissionList() {
		return permissionList;
	}

	public void setPermissionList(List<Permission> permissionList) {
		this.permissionList = permissionList;
	}

	@ManyToOne  
    @JoinColumn(name="corp_id")
	public Corporation getCorp() {
		return corp;
	}

	public void setCorp(Corporation corp) {
		this.corp = corp;
	}
	
	//该注解表示 , role是 t_user_role关系表 的维护端
	@ManyToMany(mappedBy="roleList")
	@JsonIgnore	
	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	@Column(name="name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(name="description")
	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name="creator")
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@Column(name="create_time")
	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	@Column(name="status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
}


